package com.stgl.util;

/**
 * @(#)com.casic27.platform.util.AssertUtils.java
 * 版权声明 航天光达科技有限公司, 版权所有 违者必究
 *
 *<br> Copyright:： Copyright (c) 2012
 *<br> Company： 航天光达科技有限公司
 *<br> Date：Apr 12, 2012
 *————————————————————————————————————
 *修改记录
 *    修改者：
 *    修改时间：
 *    修改原因：
 *—————————————————————————————————————
 */

import net.sf.jxls.exception.ParsePropertyException;
import net.sf.jxls.transformer.XLSTransformer;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.util.Map;


/**
 * 导出
 * 
 */
public class ExportExcel
{

    public static void download(String modalxls, Map<String, Object>resultm, HttpServletRequest req, HttpServletResponse res) throws ParsePropertyException, IOException, InvalidFormatException {
        String se= File.separator;
        XLSTransformer e=new XLSTransformer();

        String tpl=req.getRealPath(se)+se+"static"+se+modalxls;
        File folder=new File(req.getRealPath(se)+se+"static"+se+modalxls);
        if(!folder.exists()){
            System.out.print("模板不存在！");
        }

        File excelFolder=new File(req.getRealPath(se)+se+"dload");
        if(!excelFolder.exists()){
            excelFolder.mkdir();
        }

        long sysid=System.nanoTime();
        String dlname=req.getRealPath(se)+se+"dload"+se+sysid+".xlsx";
        File excelnow=new File(dlname);
        if(!excelnow.exists()){
            excelnow.createNewFile();
        }
        if(!excelnow.exists()){
            System.out.print("导出文件创建失败！");
        }
        e.transformXLS(tpl,resultm,dlname);
        downloadfile(res,req,"dload"+se+sysid+".xlsx");

    }


    /**
     * @param resp
     * @param fileurl         文件路径
     */
    public static void downloadfile(HttpServletResponse resp, HttpServletRequest request, String fileurl) {
        String se= File.separator;
        String realPath =request.getRealPath(se) ;
        String path = realPath + se + fileurl;
        File file = new File(path);
        String fileName =file.getName();

        resp.reset();
        resp.setContentType("application/octet-stream");
        resp.setCharacterEncoding("utf-8");
        resp.setContentLength((int) file.length());
        resp.setHeader("Content-Disposition", "attachment;filename=" + fileName);
        byte[] buff = new byte[1024];
        BufferedInputStream bis = null;
        OutputStream os = null;
        try {
            os = resp.getOutputStream();
            bis = new BufferedInputStream(new FileInputStream(file));
            int i = 0;
            while ((i = bis.read(buff)) != -1) {
                os.write(buff, 0, i);
                os.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                bis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
